Get premium membership and access questions with answers, video lessons as well as revision papers.

Given this class fragment, class samp { double *p; public: samp (do d) { p = (double *) malloc(sizeof(double)); if (!p) exit (1); //allocation error *p = d; } ~samp() {free(p) ;} //... }; ...

      

Given this class fragment,
class samp {
double *p;
public:
samp (do d) {
p = (double *) malloc(sizeof(double));
if (!p) exit (1); //allocation error
*p = d;
}
~samp() {free(p) ;}
//...
};
//...
samp ob1(1, ob2(0.0);
//...
ob2 = ob1;
what problem is caused by the assignment of ob1 to ob2?

  

Answers


Davis
The memory pointed to by ob2's initial value of p is now lost because this value is overwritten by the assignment.This memory thus becomes impossible to free, and the memory pointed to by ob1's p is freed twice when it is destroyed-possibly causing damage to the dynamic allocation system.
Githiari answered the question on May 12, 2018 at 17:59


Next: To illustrate exactly when an object is constructed and destructed when returned from a function, create a C++ class called who. Have who's constructor take...
Previous: Discuss the typical farm management decisions a farm operator makes

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions